﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class MinimumstockController : Controller
    {
        //
        // GET: /Minimumstock/
        private readonly IProductRepository _productRepository;
        private readonly IMinimumstockRepository _minimumstockRepository;
        private readonly IUnitOfWork _unitOfEork;
        public MinimumstockController(IProductRepository productRepository,IMinimumstockRepository minimumstockRepository,IUnitOfWork unitOfWork)
        {
            _productRepository = productRepository;
            _minimumstockRepository = minimumstockRepository;
            _unitOfEork = unitOfWork;
        }
        private IEnumerable<MinimumstockModel> GetMinimumstockModels()
        {
            return from m in _minimumstockRepository.GetAll().AsQueryable()
                   select new MinimumstockModel
                              {
                                  ID=m.ID,
                                  Productname = m.Product.Name,
                                  Minimumstock = m.Count,
                              };
        }
        public ActionResult Index()
        {
            ViewData["products"] = _productRepository.GetAll();
            return View(GetMinimumstockModels());
        }
        [HttpPost]
        [GridAction]
        public ActionResult SelectAjax()
        {
            ViewData["products"] = _productRepository.GetAll();
            return View(new GridModel(GetMinimumstockModels()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult Insert (FormCollection form,int product)
        {
            var minsock = _minimumstockRepository.Get(x => x.Product.ID == product);
            if(minsock==null)
            {
                var min = new Minimumstock
                {
                    Count = form["Minimumstock"],
                    Product = _productRepository.Get(x => x.ID == product),
                };
                if (TryUpdateModel(min, null, null, new[] { "Product" }))
                {
                    _minimumstockRepository.Add(min);
                    _unitOfEork.Commit();
                }
                return View(new GridModel(GetMinimumstockModels()));
            }
            return PartialView("_MInPage");
        }
         [HttpPost]
        [GridAction]
        public ActionResult Update(int id, int product, FormCollection form)
         {
             var min = _minimumstockRepository.Get(x => x.ID == id);
             min.Count = form["Minimumstock"];
             min.Product = _productRepository.Get(x => x.ID == product);
             if (TryUpdateModel(min, null, null, new[] { "Product" }))
             {
                 _minimumstockRepository.Update(min);
                 _unitOfEork.Commit();
             }
             return View(new GridModel(GetMinimumstockModels()));
         }
         [HttpPost]
        [GridAction]
        public ActionResult Delete(int id)
         {
             var min = _minimumstockRepository.GetById(id);
             if ((TryUpdateModel(min)))
             {
               _minimumstockRepository.Delete(min);
                 _unitOfEork.Commit();
             }
             return View(new GridModel(GetMinimumstockModels()));
         }
      
    }
}
